package com.rf.HelloSpringBoot.handler;

import static org.hamcrest.CoreMatchers.instanceOf;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rf.HelloSpringBoot.Utils.ResultUtil;
import com.rf.HelloSpringBoot.domain.Result;
import com.rf.HelloSpringBoot.enums.ResultEnum;
import com.rf.HelloSpringBoot.exception.MyException;

/**
 * 
 * @author linzhengchun
 *
 * 2017年8月1日 上午11:20:10
 */
@ControllerAdvice
public class MyExceptionHandler{
	private final static Logger logger = LoggerFactory.getLogger(MyExceptionHandler.class);
	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public Result handler (Exception e) {
		if (e instanceof MyException) {
			MyException myException = (MyException) e;
			return ResultUtil.error(myException.getCode(), myException.getMessage());
		}else {
			logger.error("【系统异常】{}", e);
			return ResultUtil.error(ResultEnum.UNKNOW_ERROR.getCode(), ResultEnum.UNKNOW_ERROR.getMsg());
		}		
	}
}
